package com.supor.aiot.mine.ui.wish;

import android.annotation.SuppressLint;
import android.app.Application;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.api.UserImpl;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.api.WishImpl;
import com.supor.aiot.mine.bean.WishBean;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

public class WishViewModel extends BaseViewModel {
    private MutableLiveData<Result> wishListData = new MutableLiveData<>();
    private MutableLiveData<Result> deleteResult = new MutableLiveData<>();
    private MutableLiveData<String> loadMoreData = new MutableLiveData<>();
    private MutableLiveData<Result> tasteData = new MutableLiveData<>();
    int page = 1;
    int limit = 5;

    public WishViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<Result> getWishListData() {
        return wishListData;
    }

    public MutableLiveData<Result> getDeleteResult() {
        return deleteResult;
    }

    public MutableLiveData<String> getLoadMoreData() {
        return loadMoreData;
    }

    @SuppressLint("CheckResult")
    public void getWishList(boolean loadMore) {
        if (loadMore) {
            page++;
        } else {
            page = 1;
            // ipsList.clear();
        }
        WishImpl.getInstance().getWishList(page, limit).subscribe(result -> {
            if (result.isSuccess()) {
                wishListData.setValue(new Result.Success(result.data.rows));
                if (result.data.rows.size() < limit || page == result.data.allpages) {
                    loadMoreData.setValue(Constants.Value.CANCEL);
                }
            } else {
                wishListData.setValue(new Result.Error("获取心愿单列表失败"));
                Logc.e("accept: failed");
            }
        }, throwable -> {
            throwable.printStackTrace();
            Logc.e("accept: failed: " + throwable.getMessage());
            wishListData.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    @SuppressLint("CheckResult")
    public void deleteWish(String wishId, int position) {
        WishImpl.getInstance().deleteWish(wishId).subscribe(result -> {
            if (result.isSuccess()) {
                deleteResult.setValue(new Result.Success(position));
            } else {
                deleteResult.setValue(new Result.Error("删除心愿单失败"));
            }
        }, throwable -> {
            throwable.printStackTrace();
            deleteResult.setValue(new Result.Error(new Exception(throwable)));
            Logc.e("accept: failed: " + throwable.getMessage());
        });
    }

    public MutableLiveData<Result> getTasteData() {
        return tasteData;
    }

    @SuppressLint("CheckResult")
    public void getTaste() {
        UserImpl.getInstance().getDictionary("TASTE", 1, 100).subscribe(result -> {
            if (result.isSuccess()) {
                tasteData.setValue(new Result.Success<>(result.data.rows));
            } else {
                tasteData.setValue(new Result.Error("getAgeList failed"));
                Logc.e("accept: failed");
            }
        }, throwable -> {
            tasteData.setValue(new Result.Error(new Exception(throwable)));
            Logc.e("getAgeList: " + throwable);
        });
    }

    /**
     * 我的心愿单
     *
     * @return
     */
    public static List<WishBean> getWishes() {
        List<WishBean> list = new ArrayList<>();
        WishBean wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年7月5日";
        wish.wishName = "糖醋排骨";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        // wish.recipeId = "";
        // wish.recipeName = "糖醋排骨";
        // wish.recipeUrl = "https://outin-1b20dde6136711eb8a4a00163e1c35d5.oss-cn-shanghai.aliyuncs.com/image/cover/9B6D8BCC766943B99A341D24BB72F74E-6-2.png";
        list.add(wish);
        wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年7月1日";
        wish.wishName = "冰糖甲鱼";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        // wish.recipeId = "";
        // wish.recipeName = "糖醋排骨";
        // wish.recipeUrl = "https://outin-1b20dde6136711eb8a4a00163e1c35d5.oss-cn-shanghai.aliyuncs.com/image/cover/9B6D8BCC766943B99A341D24BB72F74E-6-2.png";
        list.add(wish);
        wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年6月30日";
        wish.wishName = "冰糖甲鱼";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        // wish.recipeId = "2020111915104710029";
        // wish.recipeName = "冰糖甲鱼";
        // wish.recipeUrl = "https://outin-1b20dde6136711eb8a4a00163e1c35d5.oss-cn-shanghai.aliyuncs.com/image/cover/9B6D8BCC766943B99A341D24BB72F74E-6-2.png";
        list.add(wish);
        wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年6月22日";
        wish.wishName = "冰糖甲鱼";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        // wish.recipeId = "2020111915104710029";
        // wish.recipeName = "冰糖甲鱼";
        // wish.recipeUrl = "https://outin-1b20dde6136711eb8a4a00163e1c35d5.oss-cn-shanghai.aliyuncs.com/image/cover/9B6D8BCC766943B99A341D24BB72F74E-6-2.png";
        list.add(wish);
        wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年6月22日";
        wish.wishName = "冰糖甲鱼";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        list.add(wish);
        wish = new WishBean();
        wish.modelName = Constants.Value.CHEF_MACHINE;
        wish.modelId = Constants.Type.CHEF_MACHINE;
        // wish.productIconRes = R.mipmap.ic_device_chief_machine;
        wish.createTime = "2021年6月22日";
        wish.wishName = "冰糖甲鱼";
        wish.wishDescription = "口味偏甜，微辣口味偏甜，微辣口味偏甜，微辣";
        // wish.tastes = new String[]{"酸辣", "家常"};
        wish.isNotified = 1;
        wish.isSatisfied = 1;
        list.add(wish);

        return list;
    }
}
